<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>权限tree</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <link rel="stylesheet" href="../../extend/dtree/dtree.css">
    <link rel="stylesheet" href="../../extend/dtree/font/dtreefont.css">
    <script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
</head>
<body>

<div style="height: 300px;overflow: auto;">
    <ul id="roleTree" class="dtree" data-id="0"></ul>
</div>
<div class="layui-form-item">
    <div class="layui-input-block">
        <button class="layui-btn layui-btn-normal" id="saveAuth">确认保存</button>
    </div>
</div>

<script type="text/javascript">

    layui.extend({
        dtree: '/extend/dtree/dtree'
    }).use(['dtree', 'layer', 'jquery', 'form'], function () {
        let dtree = layui.dtree,
            layer = layui.layer,
            form = layui.form,
            $ = layui.jquery;
        // 初始化树
        let authTree = dtree.render({
            elem: "#roleTree",
            width: '80%',
            url: "/permission/list",// 使用url加载（可与data加载同时存在）
            initLevel: 3,
            dataStyle: "layuiStyle",  //使用layui风格的数据格式
            dataFormat: "list",  //配置data的风格为list
            method: 'get',
            checkbar: true,  // 开启复选功能
            response: {message: "msg", statusCode: 0},  //修改response中返回数据的定义
            done: function (res, $ul, first) {
                    // 根据角色id  查询角色权限id
                    let  roleId = sessionStorage.getItem("roleId");
                    $.get("/role/selectPermissionByRoleId",{roleId},function (res) {
                         //[1,2,3,4]  - > "1,2,3,4"
                        let ids = res.data.join(",")
                        console.log("permissionIds="+ids);
                        // 初始化权限树
                        dtree.chooseDataInit("roleTree", ids);
                    })
            }


        });

        $("#saveAuth").click(function () {
            //获取dtree中选中的值
            var param = dtree.getCheckbarNodesParam(authTree);
            let permissionIds = param.map(p => {
                return p.nodeId;
            }).join(',')

            let  roleId = sessionStorage.getItem("roleId");
            sessionStorage.removeItem("roleId")

            $.post("/role/addRolePermission",{roleId,permissionIds},function (res) {

                if(res.code==0){
                    layer.msg(res.msg,function () {
                        //关闭弹窗
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    })
                }else{
                    layer.msg(res.msg)
                }
            })


        })

    });

</script>

</body>
</html>